package httpCtrler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-xorm/xorm"
	st "kjjcgroup_server/model"
	sv "kjjcgroup_server/service"
	"net/http"
	"strconv"
	"time"
)

func ServiceRouter(router *gin.Engine, routerAuth *gin.RouterGroup) {
	router.GET("/service", service)
	LogMap["/service"] = "分页查询服务"
	router.GET("/serviceList", serviceList)
	LogMap["/serviceList"] = "查询服务"
	routerAuth.PUT("/serviceAdd", serviceAdd)
	LogMap["/serviceAdd"] = "添加服务"
	routerAuth.PUT("/serviceEdit", serviceEdit)
	LogMap["/serviceEdit"] = "编辑服务"
	routerAuth.DELETE("/serviceDel", serviceDel)
	LogMap["/serviceDel"] = "删除服务"
	router.GET("/serviceType", serviceType)
	LogMap["/serviceType"] = "查询服务分类"
	routerAuth.PUT("/serviceTypeAdd", serviceTypeAdd)
	LogMap["/serviceTypeAdd"] = "添加服务分类"
	routerAuth.PUT("/serviceTypeEdit", serviceTypeEdit)
	LogMap["/serviceTypeEdit"] = "编辑服务分类"
	routerAuth.DELETE("/serviceTypeDel", serviceTypeDel)
	LogMap["/serviceTypeDel"] = "删除服务分类"
}

func service(c *gin.Context) {
	ServiceId := c.Query("ServiceId")
	Recommend := c.Query("Recommend")
	Keyword := c.Query("Keyword")
	Hot := c.Query("Hot")
	ServiceTitle := c.Query("service")
	TypeId := c.Query("TypeId")
	ProfessionTypeId := c.Query("ProfessionTypeId")
	pageIdx, pageCnt := Page(c)
	if pageIdx == -1 {
		pageIdx = 0
	}
	if pageCnt == -1 {
		pageCnt = 10
	}
	mysql, err := sv.GetEngine()
	if err != nil {
		c.JSON(409, &MsgReturn{Msg: "Open DataBase Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			return
		}
	}(mysql)
	mysql.ShowSQL(true)
	var collArr []*st.ServiceMod
	var collArr1 []*st.ServiceMod
	var count int64
	if ServiceId != "" {
		err = mysql.Where("service_id=?", ServiceId).Find(&collArr)
		_, _ = mysql.Where("service_id=?", ServiceId).Update(&st.ServiceMod{ClicksNumber: collArr[0].ClicksNumber + 1})
		c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
		return
	} else if Keyword != "" {
		err = mysql.Where("service_title LIKE ? or service_sub_title  LIKE ?  or service_des  LIKE ? ", "%"+Keyword+"%", "%"+Keyword+"%", "%"+Keyword+"%").Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("service_title LIKE ? or service_sub_title  LIKE ?  or service_des  LIKE ? ", "%"+Keyword+"%", "%"+Keyword+"%", "%"+Keyword+"%").FindAndCount(&collArr1)
	} else if Recommend != "" {
		err = mysql.Where("recommend=?", Recommend).Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("recommend=?", Recommend).FindAndCount(&collArr1)
	} else if Hot != "" {
		err = mysql.Where("hot=?", Hot).Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("hot=?", Hot).FindAndCount(&collArr1)
	} else if TypeId != "" {
		err = mysql.Where("type_id LIKE ?", "%"+TypeId+"%").Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("type_id LIKE ?", "%"+TypeId+"%").FindAndCount(&collArr1)
	} else if ProfessionTypeId != "" {
		err = mysql.Where("profession_type_id=?", ProfessionTypeId).Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("profession_type_id=?", ProfessionTypeId).FindAndCount(&collArr1)
	} else if ServiceTitle != "" {
		err = mysql.Where("service_title LIKE ?", "%"+ServiceTitle+"%").Or("service_sub_title LIKE ?", "%"+ServiceTitle+"%").Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("service_title LIKE ?", "%"+ServiceTitle+"%").FindAndCount(&collArr1)
	} else {
		err = mysql.Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.FindAndCount(&collArr1)
	}
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "failed", Data: nil})
		return
	}
	rtnDt := rtnData(count, pageCnt, pageIdx, collArr)
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: rtnDt})
}

func serviceList(c *gin.Context) {
	ServiceId := c.Query("ServiceId")
	Recommend := c.Query("Recommend")
	Hot := c.Query("Hot")
	ServiceTitle := c.Query("service")
	TypeId := c.Query("TypeId")
	ProfessionTypeId := c.Query("ProfessionTypeId")

	mysql, err := sv.GetEngine()
	if err != nil {
		c.JSON(409, &MsgReturn{Msg: "Open DataBase Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			return
		}
	}(mysql)
	// mysql.ShowSQL(true)
	var collArr []*st.ServiceMod
	if ServiceId != "" {
		err = mysql.Where("service_id=?", ServiceId).Find(&collArr)
		_, _ = mysql.Where("service_id=?", ServiceId).Update(&st.ServiceMod{ClicksNumber: collArr[0].ClicksNumber + 1})
		c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
		return
	} else if Recommend != "" {
		err = mysql.Where("recommend=?", Recommend).OrderBy("create_time desc").Find(&collArr)
	} else if Hot != "" {
		err = mysql.Where("hot=?", Hot).OrderBy("create_time desc").Find(&collArr)
	} else if TypeId != "" {
		err = mysql.Where("type_id LIKE ? and Recommend = ?", "%"+TypeId+"%", 1).OrderBy("create_time desc").Find(&collArr)
	} else if ProfessionTypeId != "" {
		err = mysql.Where("profession_type_id=?", ProfessionTypeId).OrderBy("create_time desc").Find(&collArr)
	} else if ServiceTitle != "" {
		err = mysql.Where("service_title LIKE ?", "%"+ServiceTitle+"%").Or("service_sub_title LIKE ?", "%"+ServiceTitle+"%").OrderBy("create_time desc").Find(&collArr)
	} else {
		err = mysql.OrderBy("create_time desc").Find(&collArr)
	}
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
}

func serviceAdd(c *gin.Context) {
	resDt := &st.ServiceMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	//	mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	_, err = sess.Cols("type_id", "service_title", "service_sub_title", "service_des", "service_img", "service_con", "recommend", "hot", "user_account", "clicks_number", "create_time").InsertOne(&st.ServiceMod{TypeId: resDt.TypeId, ServiceTitle: resDt.ServiceTitle, ServiceSubTitle: resDt.ServiceSubTitle, ServiceDes: resDt.ServiceDes, ServiceImg: resDt.ServiceImg, ServiceCon: resDt.ServiceCon, Recommend: resDt.Recommend, Hot: resDt.Hot, UserAccount: resDt.UserAccount, ClicksNumber: resDt.ClicksNumber, CreateTime: t})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: resDt.ServiceTitle + "添加成功!"})
}

func serviceEdit(c *gin.Context) {
	resDt := &st.ServiceMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	// mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	_, err = sess.Where("service_id=?", resDt.ServiceId).Update(&st.ServiceMod{TypeId: resDt.TypeId, ServiceTitle: resDt.ServiceTitle, ServiceSubTitle: resDt.ServiceSubTitle, ServiceDes: resDt.ServiceDes, ServiceImg: resDt.ServiceImg, ServiceCon: resDt.ServiceCon, Recommend: resDt.Recommend, Hot: resDt.Hot, UserAccount: resDt.UserAccount, ClicksNumber: resDt.ClicksNumber})
	_, err = sess.Where("service_id=?", resDt.ServiceId).Cols("recommend", "hot").Update(&st.ServiceMod{Recommend: resDt.Recommend, Hot: resDt.Hot})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + resDt.ServiceTitle + " 编辑成功!"})
}

func serviceDel(c *gin.Context) {
	ServiceId := c.Query("ServiceId")
	mysql, err := sv.GetEngBom()
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			c.JSON(409, "")
			return
		}
	}(mysql)
	_, err = mysql.Where("service_id=?", ServiceId).Delete(&st.ServiceMod{})
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + ServiceId + " 删除成功!"})
}

func serviceType(c *gin.Context) {
	typeId := c.Query("tyeId")
	parentId := c.Query("parentId")
	mysql, err := sv.GetEngine()
	if err != nil {
		c.JSON(409, &MsgReturn{Msg: "Open DataBase Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			return
		}
	}(mysql)
	mysql.ShowSQL(true)

	var collArr []*st.ServiceTypeMod
	if typeId != "" {
		err = mysql.Where("type_id=?", typeId).Find(&collArr)
	} else if parentId != "" {
		err = mysql.Where("parent_id=?", parentId).Find(&collArr)
	} else {
		err = mysql.Find(&collArr)
	}
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
}

func serviceTypeAdd(c *gin.Context) {
	resDt := &st.ServiceTypeMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	//	mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()

	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	_, err = sess.Cols("type_name", "parent_id", "type_img", "type_des", "create_time", "update_time").InsertOne(&st.ServiceTypeMod{TypeName: resDt.TypeName, ParentId: resDt.ParentId, TypeImg: resDt.TypeImg, TypeDes: resDt.TypeDes, CreateTime: t, UpdateTime: t})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "添加" + resDt.TypeName + "成功!"})
}

func serviceTypeEdit(c *gin.Context) {
	resDt := &st.ServiceTypeMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	// mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	_, err = sess.Where("type_id=?", resDt.TypeId).Update(&st.ServiceTypeMod{TypeName: resDt.TypeName, ParentId: resDt.ParentId, TypeImg: resDt.TypeImg, TypeDes: resDt.TypeDes})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + strconv.Itoa(resDt.TypeId) + " 编辑成功!"})
}

func serviceTypeDel(c *gin.Context) {
	typeId := c.Query("typeId")
	mysql, err := sv.GetEngBom()
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	_, err = mysql.Where("type_id=?", typeId).Delete(&st.ServiceTypeMod{})
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + typeId + " 删除成功!"})
}
